/**
 * 角色相关js
 *
 * Created by zhangsi on 2015/10/16.
 */
var roleCtrl = new XFApp.client();
XFApp.client.prototype.role = {
    isSubmitting : false,
    successCode : 10000,
    urlItems : {
        listUrl : basePath +"/role/ajax/list",
        deleteUrl : basePath + "/role/ajax/delete"
    },
    queryPage : function(){
        var _this = this;
        var params = {
            roleName : $("#roleName").val()
        }
        if(!!_this.page){
            _this.page.page("remote",0,params);
        }else{
            _this.page = $("#page").page({
                pageSize : 10,
                remote: {
                    url: _this.urlItems.listUrl,
                    params: params,
                    success: function (data, pageIndex) {
                        if(data.code==_this.successCode){
                            var pageResult = data.result;
                            if(pageResult.itemList.length>0){
                                for(var index in pageResult.itemList){
                                    var element = pageResult.itemList[index];
                                    element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
                                }
                                $("#page_grid").html($("#dataTmp").render(pageResult.itemList));
                            }else{
                                $("#page_grid").html('<a class="list-group-item" href="javascript:void(0);">暂无角色数据</a>');
                            }
                        }else{
                            webDialogUtil.simpleAlert('数据查询失败!','温馨提示');
                        }
                    },
                    error : function(){
                        webDialogUtil.simpleAlert('数据查询失败!','温馨提示');
                    }
                },
                firstBtnText : '首页',
                lastBtnText : '尾页',
                prevBtnText : '上一页',
                nextBtnText : '下一页',
                showInfo : true,
                showJump : true,
                jumpBtnText : '跳转',
                showPageSizes : true,
                infoFormat : '共{pageCount}页,{total}项'
            });
        }
    },
    deleteRoleDialog : function(roleId){
        webDialogUtil.simpleConfrimAlert("确定要删除此角色？删除后不可恢复！",
            function(){roleCtrl.role.deleteRole(roleId);});
    },
    deleteRole : function(roleId){
        var _this = this;
        $.ajax({
            url:_this.urlItems.deleteUrl,
            data:{roleId : roleId},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(data.code == _this.successCode){
                    webDialogUtil.callbackConfrimAlert(data.msg, function(){roleCtrl.role.queryPage();});
                }else{
                    webDialogUtil.simpleAlert(data.msg);
                }
            },
            error : function() {
                webDialogUtil.simpleAlert("删除角色失败，请刷新后重试!");
            }
        });
    },
    event : function(){
        var _this = this;
        $("#submitBtn").on("click",function(){
            $('#editForm').bootstrapValidator('validate');
            if($('#editForm').data('bootstrapValidator').isValid()){
                _this.saveOrUpdate();
            }
        });
        $("#query_role").on("click",function(){
            _this.queryPage();
        });
        _this.queryPage();

    }
}
$(function(){
    roleCtrl.role.event();
});